//
//  NSLayoutConstraint+AutoScale.m
//  SmartCity
//
//  Created by ZCGC on 2017/8/22.
//  Copyright © 2017年 NRH. All rights reserved.
//

#import "NSLayoutConstraint+AutoScale.h"
#import <objc/runtime.h>


#if TARGET_OS_IOS

#import <UIKit/UIKit.h>

//如果想要判断设备是ipad，要用如下方法
BOOL _sp_isIpad()
{
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"]) {
        //iPhone
        return NO;
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        //iPod Touch
        return NO;
    }
    else if([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
        //iPad
        return YES;
    }
    return NO;
    
    //这两个防范判断不准，不要用
    //#define is_iPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    //
    //#define is_iPad (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)
}

CGFloat screenWidthScale() {
    static CGFloat scale = 1;
    static dispatch_once_t onceToken;
    
    if (_sp_isIpad()) {
        return scale;
    }
    
    dispatch_once(&onceToken, ^{
        UIInterfaceOrientation o;
        
        if (@available(iOS 13.0, *)) {
            o = [UIApplication sharedApplication].keyWindow.windowScene.interfaceOrientation;
        }else{
            
            o = [UIApplication sharedApplication].statusBarOrientation;
        }
        if (UIInterfaceOrientationIsPortrait(o)) {
            scale = [UIScreen mainScreen].bounds.size.width / 375.;
        }else if (UIInterfaceOrientationIsLandscape(o)) {
            scale = [UIScreen mainScreen].bounds.size.height / 375.;
        }
    });
    
    return scale;
}
#else
CGFloat screenWidthScale() {
    return 1;
}
#endif



@implementation NSLayoutConstraint (AutoScale)

+ (void)load{
    NSArray *originalSelectors = @[@"setConstant:"];
    
    for (NSString *selectorName in originalSelectors) {
        SEL originalSelector = NSSelectorFromString(selectorName);
        SEL swizzledSelector = NSSelectorFromString([@"sp_" stringByAppendingString:selectorName]);
        
        Method originalMethod = class_getInstanceMethod(self, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
        
        BOOL didAddMethod =
        class_addMethod(self,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(self,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
}

- (void)sp_setConstant:(CGFloat)constant {
    if (self.autoScale) {
        constant *= screenWidthScale();
    }
    return [self sp_setConstant:constant];
}

- (void)setAutoScale:(BOOL)autoScale {
    if (autoScale == self.autoScale)
        return;
    
    objc_setAssociatedObject(self, "autoScale", @(autoScale), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
    if (self.constant == 0)
        return;
    
    if (autoScale) {
        return [self sp_setConstant:self.constant * screenWidthScale()];
    }else{
        return [self sp_setConstant:self.constant / screenWidthScale()];
    }
}
- (BOOL)autoScale {
    return [objc_getAssociatedObject(self, "autoScale") boolValue];
}

@end
